﻿Imports DevExpress.XtraEditors

Public Class frmOperationServer
    Public Sub view_list()
        Dim data As DataTable
        data = mysql_query("SELECT * FROM tb_host")
        GridControlList.DataSource = data
        If data.Rows.Count > 0 Then
            SimpleButtonEdit.Enabled = True
            SimpleButtonDelete.Enabled = True
        Else
            SimpleButtonEdit.Enabled = False
            SimpleButtonDelete.Enabled = False
        End If
    End Sub

    Private Sub frmOperationServer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Cursor = Cursors.WaitCursor

        Try
            view_list()
        Catch ex As Exception
            XtraMessageBox.Show("Connection failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Cursor = Cursors.Default
    End Sub

    Private Sub SimpleButtonDelete_Click(sender As Object, e As EventArgs) Handles SimpleButtonDelete.Click
        If XtraMessageBox.Show("Are you sure want to delete this server?" & Environment.NewLine & "This action will delete all operation under this server.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Cursor = Cursors.WaitCursor

            Dim id_host As String = GridViewList.GetFocusedRowCellDisplayText("id_host").ToString

            mysql_non_query(String.Format("DELETE FROM tb_host WHERE id_host = '{0}'", id_host))
            view_list()

            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub SimpleButtonRefresh_Click(sender As Object, e As EventArgs) Handles SimpleButtonRefresh.Click
        view_list()
    End Sub

    Private Sub SimpleButtonAdd_Click(sender As Object, e As EventArgs) Handles SimpleButtonAdd.Click
        frmOperationServer_Form.id_host = -1
        frmOperationServer_Form.ShowDialog()
    End Sub

    Private Sub SimpleButtonEdit_Click(sender As Object, e As EventArgs) Handles SimpleButtonEdit.Click
        Try
            frmOperationServer_Form.id_host = CType(GridViewList.GetFocusedRowCellDisplayText("id_host").ToString, Long)
            frmOperationServer_Form.ShowDialog()
        Catch ex As Exception
            XtraMessageBox.Show("Please Select One Item For Editing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

End Class